﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace conan.Api
{
    /// <summary>
    /// 权限
    /// </summary>
    public class BasicAuth :ActionFilterAttribute
    {

        //        {
        //  "pragma": "no-cache",
        //  "date": "Tue, 26 Sep 2017 01:47:09 GMT",
        //  "server": "Microsoft-IIS/7.5",
        //  "x-aspnet-version": "4.0.30319",
        //  "x-powered-by": "ASP.NET",
        //  "content-type": "application/json; charset=utf-8",
        //  "access-control-allow-origin": "*",
        //  "cache-control": "no-cache",
        //  "content-length": "43",
        //  "expires": "-1"
        //}


        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (context.HttpContext.Request != null && context.HttpContext.Request.Headers != null && context.HttpContext.Request.Headers["Authorization"].Count > 0)
            {
                var Token = context.HttpContext.Request.Headers["Authorization"];
                if (string.IsNullOrWhiteSpace(Token))
                {
                    Meta Meta = new Meta() { State = 401, Msg = "Unauthorized" };
                    JsonResult json = new JsonResult(new
                    {
                        Meta = Meta


                    }
                    );
                    JsonSerializerSettings jsetting = new JsonSerializerSettings();
                    jsetting.NullValueHandling = NullValueHandling.Ignore;

                    jsetting.Converters.Add(
                        new Newtonsoft.Json.Converters.IsoDateTimeConverter()
                        {
                            DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
                        }
                    );

                    json.SerializerSettings = jsetting;


                    json.ContentType = "application/json; charset=utf-8";
                    context.Result = json;
                }
                else
                {
                    GenericIdentity ci = new GenericIdentity(Token);
                    ci.Label = "conan1111111";

                    context.HttpContext.User = new GenericPrincipal(ci, null);

                }
            }
            else
            {
                Meta Meta = new Meta() { State = 401, Msg = "Unauthorized" };
                JsonResult json = new JsonResult(new
                {
                    Meta = Meta


                }
                );
                JsonSerializerSettings jsetting = new JsonSerializerSettings();
                jsetting.NullValueHandling = NullValueHandling.Ignore;

                jsetting.Converters.Add(
                    new Newtonsoft.Json.Converters.IsoDateTimeConverter()
                    {
                        DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
                    }
                );

                json.SerializerSettings = jsetting;


                json.ContentType = "application/json; charset=utf-8";
                context.Result = json;
            }


            base.OnActionExecuting(context);
        }

        ////public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        ////{

        ////    //Meta Meta = new Meta() { State = 401, Msg = "Unauthorized" };
        ////    //JsonResult json = new JsonResult(new
        ////    //{
        ////    //    Meta = Meta


        ////    //}
        ////    //);
        ////    //JsonSerializerSettings jsetting = new JsonSerializerSettings();
        ////    //jsetting.NullValueHandling = NullValueHandling.Ignore;

        ////    //jsetting.Converters.Add(
        ////    //    new Newtonsoft.Json.Converters.IsoDateTimeConverter()
        ////    //    {
        ////    //        DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
        ////    //    }
        ////    //);

        ////    //json.SerializerSettings = jsetting;


        ////    //json.ContentType = "application/json; charset=utf-8";
        ////    //context.Result = json;



        ////    if (context.HttpContext.Request != null && context.HttpContext.Request.Headers != null && context.HttpContext.Request.Headers["Authorization"].Count > 0)
        ////    {
        ////        var Token = context.HttpContext.Request.Headers["Authorization"];
        ////        if (string.IsNullOrWhiteSpace(Token))
        ////        {
        ////            Meta Meta = new Meta() { State = 401, Msg = "Unauthorized" };
        ////            JsonResult json = new JsonResult(new
        ////            {
        ////                Meta = Meta


        ////            }
        ////            );
        ////            JsonSerializerSettings jsetting = new JsonSerializerSettings();
        ////            jsetting.NullValueHandling = NullValueHandling.Ignore;

        ////            jsetting.Converters.Add(
        ////                new Newtonsoft.Json.Converters.IsoDateTimeConverter()
        ////                {
        ////                    DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
        ////                }
        ////            );

        ////            json.SerializerSettings = jsetting;


        ////            json.ContentType = "application/json; charset=utf-8";
        ////            context.Result = json;
        ////        }
        ////        else
        ////        {
        ////            GenericIdentity ci = new GenericIdentity(Token);
        ////            ci.Label = "conan1111111";

        ////            context.HttpContext.User = new GenericPrincipal(ci, null);

        ////        }
        ////    }
        ////    else
        ////    {
        ////        Meta Meta = new Meta() { State = 401, Msg = "Unauthorized" };
        ////        JsonResult json = new JsonResult(new
        ////        {
        ////            Meta = Meta


        ////        }
        ////        );
        ////        JsonSerializerSettings jsetting = new JsonSerializerSettings();
        ////        jsetting.NullValueHandling = NullValueHandling.Ignore;

        ////        jsetting.Converters.Add(
        ////            new Newtonsoft.Json.Converters.IsoDateTimeConverter()
        ////            {
        ////                DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
        ////            }
        ////        );

        ////        json.SerializerSettings = jsetting;

               
        ////        json.ContentType = "application/json; charset=utf-8";
        ////        context.Result = json;
        ////    }









        ////    return base.OnActionExecutionAsync(context, next);
        ////}

       
    }
}